<template>
  <el-card class="order-container">
    <div class="order-header">
      <div class="order-title">订单详情</div>
      <div class="order-actions">
        <el-button type="primary" @click="handlePrint">打印订单</el-button>
        <el-button @click="handleBack">返回列表</el-button>
      </div>
    </div>
    
    <div class="data">
      <el-card class="data-item" shadow="hover">
        <template #header>
          <div class="card-header">
            <span>订单状态</span>
          </div>
        </template>
        <div class="card-content">
          <span class="status-badge" :class="getStatusClass(state.data.orderStatus)">
            {{ state.data.orderStatusString }}
          </span>
        </div>
      </el-card>
      
      <el-card class="data-item" shadow="hover">
        <template #header>
          <div class="card-header">
            <span>退款状态</span>
          </div>
        </template>
        <div class="card-content">
          <span>
            {{ state.data.refundStatus }}
          </span>
        </div>
      </el-card>
      
      <el-card class="data-item" shadow="hover">
        <template #header>
          <div class="card-header">
            <span>创建时间</span>
          </div>
        </template>
        <div class="card-content">
          {{ state.data.createTime }}
        </div>
      </el-card>
      
      <el-card class="data-item" shadow="hover">
        <template #header>
          <div class="card-header">
            <span>订单号</span>
          </div>
        </template>
        <div class="card-content">
          {{ state.data.orderNo }}
        </div>
      </el-card>
      
      <el-card class="data-item" shadow="hover">
        <template #header>
          <div class="card-header">
            <span>物流单号</span>
          </div>
        </template>
        <div class="card-content tracking-info">
          <span>{{ state.data.trackingNo || '暂无物流信息' }}</span>
          <el-tooltip
            effect="dark"
            content="复制物流单号"
            placement="top"
            v-if="state.data.trackingNo"
          >
            <span class="copy-btn" @click="copyTrackingNo">
              <el-icon><DocumentCopy /></el-icon>
            </span>
          </el-tooltip>
        </div>
      </el-card>
    </div>
    
    <el-table
      :data="state.tableData"
      tooltip-effect="dark"
      style="width: 100%"
      empty-text="暂无商品信息"
    >
      <el-table-column label="商品图片" width="120">
        <template #default="scope">
          <img class="goods-img" :key="scope.row.goodsId" :src="$filters.prefix(scope.row.goodsCoverImg)" alt="商品主图">
        </template>
      </el-table-column>
      <el-table-column prop="goodsId" label="商品编号" width="120"></el-table-column>
      <el-table-column prop="goodsName" label="商品名" show-overflow-tooltip></el-table-column>
      <el-table-column prop="goodsCount" label="商品数量" width="100"></el-table-column>
      <el-table-column prop="sellingPrice" label="价格" width="100">
        <template #default="scope">
          ￥{{ scope.row.sellingPrice }}
        </template>
      </el-table-column>
    </el-table>
    
    <div class="order-summary">
      <div class="summary-item">
        <span class="summary-label">商品总数：</span>
        <span class="summary-value">{{ getTotalCount() }} 件</span>
      </div>
      <div class="summary-item">
        <span class="summary-label">订单总额：</span>
        <span class="summary-value">￥{{ getTotalPrice() }}</span>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { onMounted, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { DocumentCopy } from '@element-plus/icons-vue'
import axios from '@/utils/axios'

const route = useRoute()
const router = useRouter()
const { id } = route.query

const state = reactive({
  data: {
    orderNo: '',
    createTime: '',
    orderStatus: 0,
    orderStatusString: '',
    refundStatus: 0,
    trackingNo: '',
    newBeeMallOrderItemVOS: []
  },
  tableData: []
})

onMounted(() => {
  axios.get(`/orders/${id}`).then(res => {
    console.log(res)
    state.data = res
    state.tableData = res.newBeeMallOrderItemVOS || []
  })
})

// 获取订单状态样式类
const getStatusClass = (status) => {
  const statusMap = {
    1: 'status-pending',
    2: 'status-paid',
    3: 'status-shipped',
    4: 'status-completed'
  }
  return statusMap[status] || 'status-pending'
}

// 获取退款状态样式类
const getRefundStatusClass = (status) => {
  const statusMap = {
    0: '',
    1: 'status-refunding',
    2: 'status-refunded',
    3: 'status-refund-failed'
  }
  return statusMap[status] || ''
}

// 获取退款状态文本
const getRefundStatusText = (status) => {
  const statusMap = {
    0: '无退款',
    1: '退款中',
    2: '已退款',
    3: '退款失败'
  }
  return statusMap[status] || '无退款'
}

// 计算商品总数
const getTotalCount = () => {
  return state.tableData.reduce((total, item) => total + (item.goodsCount || 0), 0)
}

// 计算订单总额
const getTotalPrice = () => {
  return state.tableData.reduce((total, item) => total + ((item.sellingPrice || 0) * (item.goodsCount || 0)), 0)
}

// 复制物流单号
const copyTrackingNo = () => {
  if (!state.data.trackingNo) return
  
  navigator.clipboard.writeText(state.data.trackingNo)
    .then(() => {
      ElMessage.success('物流单号已复制到剪贴板')
    })
    .catch(() => {
      // 降级方案
      const textarea = document.createElement('textarea')
      textarea.value = state.data.trackingNo
      document.body.appendChild(textarea)
      textarea.select()
      document.execCommand('copy')
      document.body.removeChild(textarea)
      ElMessage.success('物流单号已复制到剪贴板')
    })
}

// 打印订单
const handlePrint = () => {
  ElMessage.info('打印功能开发中...')
}

// 返回列表
const handleBack = () => {
  router.back()
}
</script>

<style scoped>
.order-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e6e6e6;
}

.order-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.order-actions {
  display: flex;
  gap: 10px;
}

.data {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 30px;
}

.data .data-item {
  flex: 1;
  min-width: 200px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.data .data-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}

.card-content {
  font-size: 16px;
  margin-top: 8px;
}

.status-badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-pending {
  background-color: #fff7e6;
  color: #fa8c16;
}

.status-paid {
  background-color: #f0f9ff;
  color: #1890ff;
}

.status-shipped {
  background-color: #f0f9ff;
  color: #1890ff;
}

.status-completed {
  background-color: #f6ffed;
  color: #52c41a;
}

.status-refunding {
  background-color: #fff2e8;
  color: #fa541c;
}

.status-refunded {
  background-color: #f6ffed;
  color: #52c41a;
}

.status-refund-failed {
  background-color: #fff2f0;
  color: #ff4d4f;
}

.tracking-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.copy-btn {
  cursor: pointer;
  color: #1890ff;
}

.copy-btn:hover {
  color: #40a9ff;
}

.goods-img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
}

.el-table {
  border: 1px solid #EBEEF5;
  border-radius: 8px;
  overflow: hidden;
  border-bottom: none;
}

:deep(.has-gutter) th {
  background-color: #f5f7fa;
  font-weight: 600;
  border-right: 1px solid #EBEEF5;
}

:deep(.has-gutter) th:last-child {
  border-right: none;
}

:deep(.el-table__row) td {
  border-right: 1px solid #EBEEF5;
  text-align: center;
}

:deep(.el-table__row) td:last-child {
  border-right: none;
}

.order-summary {
  margin-top: 20px;
  padding: 15px;
  background-color: #f9f9f9;
  border-radius: 8px;
  display: flex;
  justify-content: flex-end;
}

.summary-item {
  display: flex;
  align-items: center;
  margin-left: 20px;
  font-size: 16px;
}

.summary-label {
  color: #606266;
  margin-right: 8px;
}

.summary-value {
  font-weight: 600;
  color: #303133;
}

@media (max-width: 768px) {
  .data {
    flex-direction: column;
  }
  
  .data .data-item {
    min-width: 100%;
  }
  
  .order-actions {
    flex-wrap: wrap;
  }
  
  .order-header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
  
  .order-summary {
    flex-direction: column;
    gap: 10px;
  }
  
  .summary-item {
    margin-left: 0;
    justify-content: space-between;
  }
}
</style>